package com.sunjs.web.controller;

import com.sunjs.model.Users;
import com.sunjs.web.service.FeignBaseServer;
import com.sunjs.web.service.FeignOrderServer;
import com.sunjs.web.service.FeignZuulServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author sunguoliang
 * @create 2018-09-25 下午6:39
 */
@RestController
@RequestMapping("/web/users")
public class UsersController {

    @Autowired
    private FeignBaseServer feignBaseServer;
    @Autowired
    private FeignOrderServer feignOrderServer;
    @Autowired
    private FeignZuulServer feignZuulServer;

    /**
    * 通过网管进行访问的
    * http://localhost:9001/web/users/zuul/findAll
    * @Author sunguoliang
    * @Date 2018/9/26 上午10:23
    */
    @RequestMapping("/zuul/findAll")
    public List<Users> findAll() {
        return feignZuulServer.findAll();
    }

    /**
     * 通过网管进行访问的
     * http://localhost:9001/web/users/zuul/findBy?id=1
     * @Author sunguoliang
     * @Date 2018/9/26 上午10:23
     */
    @RequestMapping("/zuul/findBy")
    public Users findBy(Integer id) {
        return feignZuulServer.findBy(id);
    }

    /**
     * 通过Base服务进行访问的
     * http://localhost:9001/web/users/findAll
     * @Author sunguoliang
     * @Date 2018/9/26 上午10:23
     */
    @RequestMapping("/findAll")
    public List<Users> findAll2() {
        return feignBaseServer.findAll();
    }

    /**
     * 通过Order服务进行访问的
     * http://localhost:9001/web/users/findBy?id=1
     * @Author sunguoliang
     * @Date 2018/9/26 上午10:23
     */
    @RequestMapping("/findBy")
    public Users findBy2(Integer id) {
        return feignOrderServer.findBy(id);
    }



    @Autowired
    RestTemplate restTemplate;

    private final static String serverURI = "http://order/";
    /**
    * 测试负载均衡
    * http://192.168.0.166:9001/web/users/findBy2?id=1
    * @Author sunguoliang
    * @Date 2018/9/26 上午11:46
    */
    @RequestMapping("/findBy2")
    public Users findBy3(Integer id) {
        ResponseEntity<Users> list = restTemplate.getForEntity(serverURI+"users/findBy?id="+id,Users.class);
        return list.getBody();
    }
}